/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestionarCategoriasUI.java
 *
 * Created on 10/06/2009, 09:30:20 PM
 */

package CRM_UI;

import DAO.*;
import LogicaNegocio.*;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author LÂ¡z
 */
public class GestionarCategoriasUI extends javax.swing.JFrame {

    Vector <Vector> categoriasTabla; //filas de tabla
    Vector <String> nombreColumnas; //columnas de la tabla
    String idSeleccionado;


    public GestionarCategoriasUI() {
        categoriasTabla= new Vector<Vector>();
        inicializarColumnas();
        consultarCategorias();
        initComponents();
        setLocationRelativeTo(null);
    }


     public void inicializarColumnas()
    {
        nombreColumnas= new Vector<String>();
        nombreColumnas.addElement("Codigo");
        nombreColumnas.addElement("Nombre");

    }

     public void consultarCategorias()
    {
        DAOCategoria daoCategoria= new DAOCategoria();
        Vector<Categoria> vectorCat= new Vector<Categoria>();
        vectorCat = daoCategoria.consultarCategorias();

        System.out.println(vectorCat.size());

        for(int i=0; i<vectorCat.size(); i++)
        {
            Vector categoria= new Vector() ; //cada fila o tupla de la tabla
            categoria.addElement(vectorCat.elementAt(i).getCodigoCategoria());
            categoria.addElement(vectorCat.elementAt(i).getNombreCategoria());


            categoriasTabla.addElement(categoria);
        }


    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaCategoria = new javax.swing.JTable(categoriasTabla, nombreColumnas);
        jPanel2 = new javax.swing.JPanel();
        botonCrear = new javax.swing.JButton();
        labelCatId = new javax.swing.JLabel();
        labelCatNombre = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        botonModificar = new javax.swing.JButton();
        botonEliminar = new javax.swing.JButton();
        textFieldCatId = new javax.swing.JTextField();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Categorias"));

        /*
        tablaCategoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        */
        tablaCategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaCategoriaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tablaCategoria);

        jScrollPane2.setViewportView(jScrollPane1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Modificar"));

        botonCrear.setText("crear");
        botonCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCrearActionPerformed(evt);
            }
        });

        labelCatId.setText("Identificador:");

        labelCatNombre.setText("Nombre:");

        botonModificar.setText("modificar");
        botonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarActionPerformed(evt);
            }
        });

        botonEliminar.setText("eliminar");
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        textFieldCatId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textFieldCatIdActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelCatId)
                    .addComponent(labelCatNombre))
                .addGap(37, 37, 37)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textFieldCatId, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(148, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(74, Short.MAX_VALUE)
                .addComponent(botonCrear)
                .addGap(32, 32, 32)
                .addComponent(botonModificar)
                .addGap(26, 26, 26)
                .addComponent(botonEliminar)
                .addGap(185, 185, 185))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelCatId))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(14, 14, 14)
                        .addComponent(textFieldCatId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelCatNombre)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonModificar)
                    .addComponent(botonCrear)
                    .addComponent(botonEliminar))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        DAOCampaNaArticulo daoCampanaArticulo= new DAOCampaNaArticulo();    
        DAOProducto daoProducto = new DAOProducto();
        DAOServicio daoServicio = new DAOServicio();
        DAOArticulo daoArticulo= new DAOArticulo();
        DAOCategoria daoCategoria = new DAOCategoria();

        //int i = daoCategoria.eliminarCategoria(idSeleccionado);

        //if(i==1) {
        //    JOptionPane.showMessageDialog(null, "EliminaciÃ³n exitosa", "Eliminar CategorÃ­a", JOptionPane.INFORMATION_MESSAGE);
        //}

        //if(i==-1)
        //{
            int option = JOptionPane.showConfirmDialog(null, "Â¿EstÃ¡ seguro que desea borrar la categorÃ­a y los productos y/o servicios asociados a ella?", "Eliiminar CategorÃ­a y sus Asociaciones" , JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if(option == 0)
            {      String codProducto=daoCategoria.eliminarServicioConCategoria(idSeleccionado);
                   String codServicio= daoCategoria.eliminarProductoConCategoria(idSeleccionado);
                   int a=daoCampanaArticulo.eliminarCampaNaArticulo(codServicio);
                   int b=daoCampanaArticulo.eliminarCampaNaArticulo(codProducto);
                   int c=daoProducto.eliminarProducto(codProducto);
                   int d=daoServicio.eliminarServicio(codServicio);
                   //int e=daoArticulo.eliminarArticulo(codProducto);
                   //int f=daoArticulo.eliminarArticulo(codServicio);
                   //int j=daoCategoria.eliminarCategoria(idSeleccionado);
                   System.out.println("producto " + codProducto +" art "+codServicio);
                   System.out.println("elCAMServ " + a +" elCAMPro "+b);
                   System.out.println("elproducto " + c +" elserv "+d);
                   //System.out.println("elartPro " + e +" y eliARTserv "+f+" elcat "+j);
                   if(a==1){
                   JOptionPane.showMessageDialog(null, "Se ha eliminado la CategorÃ­a "+ idSeleccionado+" y sus asociaciones exitosamente", "Eliminar CategorÃ­a y Asociaciones", JOptionPane.INFORMATION_MESSAGE);
                   }
            }
        //}
        //daoCategoria.ConectarseABd();
        consultarCategorias();
        //initComponents();
}//GEN-LAST:event_botonEliminarActionPerformed

    private void botonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarActionPerformed
        DAOCategoria daoCategoria= new DAOCategoria();

        Categoria categoria= new Categoria(textFieldCatId.getText(), jTextField2.getText());

        int i= daoCategoria.modificarNombreCategoria(categoria);
        if(i==1) {
            JOptionPane.showMessageDialog(null, "Se ha modificado  "+i+" CategorÃ­a", "CategorÃ­a Modificada", JOptionPane.INFORMATION_MESSAGE);
            consultarCategorias();
        }

        else{
            JOptionPane.showMessageDialog(this, "Error de modificaciÃ³n .", "CategorÃ­a no Modificada", JOptionPane.ERROR_MESSAGE);
        }

        System.out.print("Se han modificado " + i + " categoria"+" en la BD\n");

}//GEN-LAST:event_botonModificarActionPerformed

    private void botonCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCrearActionPerformed
        int i=0;
        DAOCategoria daoCat= new DAOCategoria();
        Categoria categoria= new Categoria(textFieldCatId.getText(), jTextField2.getText());
        i= daoCat.guardarCategoria(categoria);

        if(i==1) {
            JOptionPane.showMessageDialog(null, "Se ha insertado  "+i+" nueva CategorÃ­a", "Nueva CategorÃ­a", JOptionPane.INFORMATION_MESSAGE);

        } else{
            JOptionPane.showMessageDialog(this, "Error de creaciÃ³n .", "CategorÃ­a no Creada", JOptionPane.ERROR_MESSAGE);
        }


        System.out.print("Se han insertado " + i + " categoria"+" a la BD\n");
        consultarCategorias();

}//GEN-LAST:event_botonCrearActionPerformed

    private void tablaCategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaCategoriaMouseClicked
        int fila= tablaCategoria.getSelectedRow();
        idSeleccionado=tablaCategoria.getValueAt(fila, 0).toString();
        textFieldCatId.setText(idSeleccionado);

        jTextField2.setText(tablaCategoria.getValueAt(fila, 1).toString());
}//GEN-LAST:event_tablaCategoriaMouseClicked

    private void textFieldCatIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textFieldCatIdActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_textFieldCatIdActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GestionarCategoriasUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCrear;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonModificar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel labelCatId;
    private javax.swing.JLabel labelCatNombre;
    private javax.swing.JTable tablaCategoria;
    private javax.swing.JTextField textFieldCatId;
    // End of variables declaration//GEN-END:variables

}
